<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Model_auth extends CI_Model {

	public function __construct() {
		parent::__construct();
		
		$this->load->helper(array('form','captcha','array'));
		$this->load->library(array('session','table','tank_auth'));
		$this->load->database();
		
		}
	
	public function _login ($data) {
		return $this->tank_auth->login($data['username'],$data['password'],true,true,false);
	}
	
	public function _logout() {
		$this->tank_auth->logout();
	}
		
	public function _logged_in() {
		return ($this->session->userdata('user_id')!='');
	}
	
	public function _get_user_id() {
		return $this->session->userdata('user_id');
	}
	
	public function _get_usertype() {
		return $this->session->userdata('usertype');
	}
		
	public function _get_usertype_id() {
		return $this->session->userdata('usertype_id');
	}
	
	public function _must_logged_in ( $as_type = 'any', $redirect_to='auth/') {
		//redirect jika belum login atau login sebagai user dgn tipe lain
		if (!($this->_logged_in()&&($this->_get_usertype()==$as_type||$as_type=='any'))) {
			redirect($redirect_to,'location');}
	}
	
	public function _must_not_logged_in ($redirect_to='default',$act='redirect') {
		if ($redirect_to == 'default') 
			$redirect_to=$this->model_auth->_get_usertype();
		if ($this->_logged_in()) {
			if ($act=='redirect') {
				redirect($redirect_to,'location'); }
			else if ($act=='error_register') {
				show_error('Silahkan logout terlebih dahulu untuk dapat melakukan pendaftaran.');
			}
			
			}
	}
	
	
	
	
	
}